﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using System.Text.RegularExpressions;

namespace BYManageInfoSystem
{
    public partial class Frm_MonthRateReference : DockContent
    {
        private DGBY.BLL.DG_MonthRateReferenceTab m_billDG_MonthRateReferenceTab = new DGBY.BLL.DG_MonthRateReferenceTab();
        private int m_nDelIndex = -1;
        public Frm_MonthRateReference()
        {
            InitializeComponent();
        }

        private void Frm_MonthRateReference_Load(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        private void ShowAllRecord(string strWhere)
        {     
            ultraGrid1.DataSource = null;
            DataSet ds = m_billDG_MonthRateReferenceTab.GetList(strWhere);

            ultraGrid1.DataSource = ds.Tables[0];
            ultraGrid1.DisplayLayout.Bands[0].Columns["DG_MonthRateReferenceTabID"].Hidden = true;
        }

        private void ultraGrid1_InitializeLayout(object sender, InitializeLayoutEventArgs e)
        {
            ClassUltraGridCommonSet.ultraGrid1_InitializeRowFilter(e);

            foreach (UltraGridColumn column2 in ultraGrid1.DisplayLayout.Bands[0].Columns)
            {
                string str = column2.Header.Caption;
                if (str == "所属年月")
                {
                    column2.CellAppearance.BackColor = Color.Gray;
                    column2.CellActivation = Activation.ActivateOnly;
                }
            }

        }

        private void InitColumnWidth()
        {
            ultraGrid1.DisplayLayout.Bands[0].Columns["所属年月"].Width = 90;
            ultraGrid1.DisplayLayout.Bands[0].Columns["美元汇率基准值"].Width = 120;
            ultraGrid1.DisplayLayout.Bands[0].Columns["港币汇率基准值"].Width = 120;
        }

        private void buttonProduce_Click(object sender, EventArgs e)
        {
            try
            {
                string strYear = this.dateTimePicker1.Value.Date.Year.ToString();
                string strMonth = this.dateTimePicker1.Value.Date.Month.ToString().PadLeft(2, '0');

                string strYearMonth = strYear + strMonth;
                int nYearMonth = Convert.ToInt32(strYearMonth, 10);

                DGBY.Model.DG_MonthRateReferenceTab modelMonthRateReferenceTab = m_billDG_MonthRateReferenceTab.GetModelByYearMonth(nYearMonth);
                if (null == modelMonthRateReferenceTab)
                {
                    if (IsNull(this.errorProvider1, this.textDollarRate))
                    {
                        return;
                    }

                    if (IsNull(this.errorProvider1, this.textHKRate))
                    {
                        return;
                    }

                    DGBY.Model.DG_MonthRateReferenceTab modelNewMonthRateReferenceTab = new DGBY.Model.DG_MonthRateReferenceTab();
                    modelNewMonthRateReferenceTab.RateYearMonth = nYearMonth;
                    modelNewMonthRateReferenceTab.DollarRate = Convert.ToDecimal(this.textDollarRate.Text);
                    modelNewMonthRateReferenceTab.HKRate = Convert.ToDecimal(this.textHKRate.Text);

                    int nID = m_billDG_MonthRateReferenceTab.Add(modelNewMonthRateReferenceTab);
                    Frm_Main.Log2DB(this.Text, "添加", nID.ToString());
                    ShowAllRecord(" DG_MonthRateReferenceTabID = '" + nID.ToString() + "'");
                }
                else
                {
                    MessageBox.Show("已存在" + nYearMonth.ToString() + "的记录", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch { }
        }

        private void buttonShowAll_Click(object sender, EventArgs e)
        {
            ShowAllRecord("");
        }

        public bool IsNull(ErrorProvider errorProvider, TextBox TextBox)
        {
            if (TextBox.Text == "")
            {
                errorProvider1.SetError(TextBox, "内容不能为空！");
                return true;
            }
            else if (!IsDecimal(TextBox.Text))
            {
                errorProvider1.SetError(TextBox, "必须输入数字");
                return true;
            }
            else
            {
                errorProvider1.SetError(TextBox, "");
                return false;
            }
        }

        private bool IsDecimal(string strInput)
        {
            bool bRet = Regex.IsMatch(strInput, @"([1-9]+[0-9]*(\.[0-9]+))|(0(\.[0-9]+))");
            return bRet;
        }

        private bool ModifyGoodsDetail(int nRowIndex)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[nRowIndex];
                int nSelectDG_MonthRateReferenceTabID = Convert.ToInt32(CurrentUGR.Cells["DG_MonthRateReferenceTabID"].Value.ToString(), 10);


                DGBY.Model.DG_MonthRateReferenceTab modelMonthRateReferenceTab = m_billDG_MonthRateReferenceTab.GetModel(nSelectDG_MonthRateReferenceTabID);
                if (null == modelMonthRateReferenceTab)
                    return false;

                modelMonthRateReferenceTab.DollarRate = Convert.ToDecimal(CurrentUGR.Cells["美元汇率基准值"].Value.ToString());
                modelMonthRateReferenceTab.HKRate = Convert.ToDecimal(CurrentUGR.Cells["港币汇率基准值"].Value.ToString());

                m_billDG_MonthRateReferenceTab.Update(modelMonthRateReferenceTab);
                Frm_Main.Log2DB(this.Text, "修改", modelMonthRateReferenceTab.DG_MonthRateReferenceTabID.ToString());
                return true;

            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void ultraGrid1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {

                case Keys.Up:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex > 0)
                            nIndex--;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.AboveCell, false,
                          false);
                        //if(nIndex != 0)

                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Down:
                    {
                        UltraGridCell gridCell = this.ultraGrid1.ActiveCell;
                        if (null == gridCell)
                            return;

                        int nColumnIndex = this.ultraGrid1.ActiveCell.Column.Index;
                        int nIndex = ultraGrid1.ActiveRow.Index;
                        if (nIndex < ultraGrid1.Rows.Count - 1)
                            nIndex++;
                        this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                          false);
                        this.ultraGrid1.PerformAction(UltraGridAction.BelowCell, false,
                          false);
                        e.Handled = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].IgnoreRowColActivation = true;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activation = Activation.AllowEdit;
                        ultraGrid1.Rows[nIndex].Cells[nColumnIndex].Activate();
                        this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                          false);
                        break;
                    }

                case Keys.Right:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.NextCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
                case Keys.Left:
                    this.ultraGrid1.PerformAction(UltraGridAction.ExitEditMode, false,
                      false);
                    this.ultraGrid1.PerformAction(UltraGridAction.PrevCellByTab, false,
                      false);
                    e.Handled = true;
                    this.ultraGrid1.PerformAction(UltraGridAction.EnterEditMode, false,
                      false);
                    break;
            }
        }

        private void ultraGrid1_BeforeRowUpdate(object sender, CancelableRowEventArgs e)
        {
            try
            {
                UltraGridRow CurrentUGR;
                CurrentUGR = ultraGrid1.Rows[e.Row.Index];

                ModifyGoodsDetail(e.Row.Index);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                e.Cancel = true;
            }
        }

        private void 删除一条ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            int nIndex = m_nDelIndex;
            if (m_nDelIndex >= 0)
            {
                DialogResult dr = MessageBox.Show("确实要删除该记录么!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }

                if (ultraGrid1.ActiveRow != null)
                {
                    UltraGridRow CurrentUGR;
                    CurrentUGR = ultraGrid1.Rows[nIndex];
                    ultraGrid1.ActiveRow.Delete(false);
                }
            }
            else
            {
                MessageBox.Show("请选中要删除的记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void ultraGrid1_AfterCellActivate(object sender, EventArgs e)
        {
            int iIndex = this.ultraGrid1.ActiveCell.Row.Index;
            if (iIndex < 0)
                return;
            m_nDelIndex = ultraGrid1.ActiveRow.Index;

        }

        private void ultraGrid1_BeforeRowsDeleted(object sender, BeforeRowsDeletedEventArgs e)
        {
            e.DisplayPromptMsg = false;

            UltraGridRow ulActiveRow = ultraGrid1.ActiveRow;
            if (ulActiveRow == null)
                return;

            int nSelectDG_MonthRateReferenceTabID = Convert.ToInt32(ulActiveRow.Cells["DG_MonthRateReferenceTabID"].Value.ToString(), 10);
            DGBY.Model.DG_MonthRateReferenceTab modelMonthRateReferenceTab = m_billDG_MonthRateReferenceTab.GetModel(nSelectDG_MonthRateReferenceTabID);
            if (null == modelMonthRateReferenceTab)
            {    MessageBox.Show("数据库不存在该记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        
            if (MessageBox.Show("确实要删除该条记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
            {
                try
                {

                   m_billDG_MonthRateReferenceTab.Delete(nSelectDG_MonthRateReferenceTabID);
                    Frm_Main.Log2DB(this.Text, "删除", nSelectDG_MonthRateReferenceTabID.ToString());

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    e.Cancel = true;
                }
            }
            else
            {
                e.Cancel = true;
            }
        }

        private void buttonExportToExcel_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)ultraGrid1.DataSource;

            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    ultraGridExcelExporter1.Export(ultraGrid1, outFile);
                }
            }
        }           
    }
}
